package edu.ccut.saturn.basecomponent.database.facade;

import java.sql.Connection;

import edu.ccut.saturn.basecomponent.database.IInsertSQLBuilder;
import edu.ccut.saturn.basecomponent.database.ISaturnSQL;
import edu.ccut.saturn.basecomponent.database.ISaturnSQLBuilder;
import edu.ccut.saturn.basecomponent.database.SaturnSQLBuilderFactory;
import edu.ccut.saturn.basecomponent.database.SaturnSQLHandler;
import edu.ccut.saturn.basecomponent.database.util.SaturnDataUtil;
import edu.ccut.saturn.component.SaturnData;

public class Insert {
	public int execute(SaturnData saturnData,Connection conn) throws Exception {
		 String dbType = SaturnDataUtil.getStandardDBType(saturnData);
		 ISaturnSQLBuilder builder = SaturnSQLBuilderFactory.getSaturnSQLBuilder(dbType);
			if (builder != null) {
				//感觉不需要Type
				IInsertSQLBuilder insertSQLBuilder = builder.getInsertSQLBuilder(getBuilderType(saturnData));
				
				ISaturnSQL saturnSQL = insertSQLBuilder.build(saturnData, conn);
				int result = SaturnSQLHandler.executeUpdate(saturnSQL, conn);
				return result;
			}
			return 0;
	}
	private String getBuilderType(SaturnData saturnData){
		//如果有大数据
		if(SaturnDataUtil.hasLOB(saturnData)){
			return ISaturnSQL.INSERT_LOB_OPERATION;
		}else{
			//否则
			return ISaturnSQL.INSERT_OPERATION;
		}
	}
//	private boolean hasLOB(SaturnData saturnData){
//		IDataInfo dataInfo = saturnData.getSaturnDataInfo();
//		for(String key :dataInfo.keySet()){
//			IDataItem dataItem = dataInfo.getDataItem(key);
//			if(OracleAttribute.isLOB(dataItem.getType())){
//				if(saturnData.get(key)!=null){
//					return true;
//				}
//			}
//		}
//		return false;
//	}
}
